﻿using System.Web.Mvc;
using System.Web.Security;
using BisinessLogic;
using Domain.Entities;
using Web.Models;

namespace Web.Controllers
{
    public class CheckoutController : Controller
    {
       // readonly 
     
        private DataManager dataManager;

        public CheckoutController(DataManager dataManager)
        {
            this.dataManager = dataManager;
        }


        public ActionResult Complete(int id)
        {
            // Validate customer owns this order
            bool isValid = true;/*storeDB.Orders.Any(
                o => o.OrderId == id &&
                o.Username == User.Identity.Name);
                */
            if (isValid)
            {
                return View(id);
            }
            else
            {
                return View("Error");
            }
        }

        public ActionResult AddressAndPayment()
        {
            var cart = ShoppingCart.GetCart(this.HttpContext);

         

            string fname="";
            string lname="";
            string mname = "";

            if (User.Identity.IsAuthenticated)
            {
                var currentUserId = (int)Membership.GetUser().ProviderUserKey;
                var user = dataManager.Users.GetUserById(currentUserId);
                fname = user.FirstName;
                lname = user.LastName;
                mname = user.MiddleName;
            }
            var order = new Order
            {
                FirstName = fname,
                LastName = lname,
                MiddleName = mname,
                Total = cart.GetTotal()
            };

            return View(order);
        }

        [HttpPost]
        public ActionResult AddressAndPayment(Order order)
        {

            if (ModelState.IsValid)
            {
               order.Username = User.Identity.Name;
               var cart = ShoppingCart.GetCart(this.HttpContext);
               int orderId = cart.CreateOrder(order);
                return RedirectToAction("Complete",
                       new { id = orderId });
            }

            return View(order);
           

           /*
            var order = new Order();
            TryUpdateModel(order);

            try
            {
                if (string.Equals(values["PromoCode"], PromoCode,
                    StringComparison.OrdinalIgnoreCase) == false)
                {
                    return View(order);
                }
                else
                {
                    order.Username = User.Identity.Name;
                    order.OrderDate = DateTime.Now;

                    //Save Order
                    storeDB.Orders.Add(order);
                    storeDB.SaveChanges();
                    //Process the order
                    var cart = ShoppingCart.GetCart(this.HttpContext);
                    cart.CreateOrder(order);

                    return RedirectToAction("Complete",
                        new { id = order.OrderId });
                }
            }
            catch
            {
                //Invalid - redisplay with errors
                return View(order);
            }
         */
         //   return View("Error");
        }
          
    }
}
